﻿using System;
using System.Globalization;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class UpdateClient : System.Web.UI.Page
    {
        private ClientEntity _client;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (!IsPostBack)
                LoadUpdateClient();
        }

        private void LoadUpdateClient()
        {
            var id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                var dataTable = ClientProfileLogic.SelectClientByClientId(id);
                textClientId.Text = id;
                textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
                textFullName.Text = dataTable.Rows[0]["FullName"].ToString();
                textCompany.Text = dataTable.Rows[0]["CompanyName"].ToString();
                textAddress.Text = dataTable.Rows[0]["Address"].ToString();
                textPhoneNumber.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
                textEmail.Text = dataTable.Rows[0]["Email"].ToString();
            }
        }

        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            _client = new ClientEntity
                         {
                             ClientId = int.Parse(textClientId.Text.Trim(), CultureInfo.InvariantCulture),
                             FullName = Server.HtmlEncode(textFullName.Text),
                             CompanyName = Server.HtmlEncode(textCompany.Text),
                             Address = Server.HtmlEncode(textAddress.Text),
                             PhoneNumber = Server.HtmlEncode(textPhoneNumber.Text),
                             Email = Server.HtmlEncode(textEmail.Text)
                         };
            var result = ClientProfileLogic.UpdateClient(_client);
            if (result!=null)
            {
                Response.Redirect("ListClient.aspx");
            }
            UserEventEntity(result);
        }

        //event of insert client
        private void UserEventEntity(string userEvent)
        {
            // Save event of insert client
            var userEventEntity = new EventEntity
            {
                UserName = CommonLogic.ConvertToString(Session["UserName"]),
                EventType = (int)EventTypeList.UserEvent,
                OnDate = DateTime.Now,
                Description = "User Insert client " + userEvent,
            };
            EventTracer.SaveEvents(userEventEntity);
            labelResultMessage.Visible = true;
            labelResultMessage.Text = userEvent;
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.UpdateClient;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }

    }
}
